package com.lgl.scsheader.sameCityChart.service;

import com.lgl.scsapi.base.dao.DaoSupport;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Constants;
import com.lgl.scsapi.util.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

/**
 * 订单城市图表service
 */

@Service("orderCityChartService")
public class OrderCityChartService {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/**
	 * 获取热门城市近一年的订单数量
	 * @return 热门城市近一年的订单数量
	 */
	public List<PageData> listHotCityOrderNumber(){
		PageData hotCityParam = new PageData(6);
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR), m = calendar.get(Calendar.MONTH) + 1;
		//获取年份后2位
		year = Integer.parseInt(String.valueOf(year).substring(2));
		int lastYear = year - 1;
		String month = m < 10 ? ("0" + m) : String.valueOf(m);
		hotCityParam.put("startDate",lastYear + "-" + month + "-01 00:00:00");
		StringBuilder countSql = new StringBuilder();
		for (int i = m; i <= 12; i ++){
			String lastMonth = i < 10 ? ("0" + i) : String.valueOf(i);
			String lastYearMonth = lastYear + lastMonth;
			countSql.append("COUNT(IF(DATE_FORMAT(co.so_order_date, '%y%m') = '" + lastYearMonth + "', 1, NULL)) '" + lastYearMonth + "',");
		}
		for (int i = 1; i <= m; i ++){
			String thisMonth = i < 10 ? ("0" + i) : String.valueOf(i);
			String thisYearMonth = year + thisMonth;
			countSql.append("COUNT(IF(DATE_FORMAT(co.so_order_date, '%y%m') = '" + thisYearMonth + "', 1, NULL)) '" + thisYearMonth + "',");
		}
		hotCityParam.put("countSql",countSql.toString());
		hotCityParam.put("hotCityCodeArray", Constants.hotCityMap.values());
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listHotCityOrderNumber", hotCityParam);
	}
	/**
	 * 获取热门城市订单总数
	 * @return 热门城市订单总数
	 */
	public List<PageData> listHotCityOrderTotal(){
		PageData hotCityParam = new PageData(4);
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR), m = calendar.get(Calendar.MONTH) + 1;
		String month = m < 10 ? ("0" + m) : String.valueOf(m);
		int lastYear = year - 1;
		hotCityParam.put("startDate",lastYear + "-" + month + "-01 00:00:00");
		hotCityParam.put("hotCityCodeArray",Constants.hotCityMap.values());
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listHotCityOrderTotal", hotCityParam);
	}

	/**
	 * 获取今年订单量最多的6座城市及其订单量、及这6座城市上一年的订单总量，
	 * 比如，今年订单量最多的6座城市是：A,B,C,D,E,F，
	 * 则取A,B,C,D,E,F城市名称及这6座城市今年和去年的订单总量数据
	 * @param mostCityPd 统计参数，如统计的某年年份
	 * @return 最近2年订单量最多的城市及订单总量
	 */
	public List<PageData> listMostOrderTotalCity(PageData mostCityPd){
		Tools.initThisLastParam(mostCityPd);
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listMostOrderTotalCity", mostCityPd);
	}

	/**
	 * 获取今年和去年订单量最多的6座城市及其订单量，注意与getMostOrderTotalCity的区别
	 * 比如：今年订单量最多的城市，可能是：A,B,C三座城市，则取A,B,C三座城市名称及其今年的订单总量数据，
	 *      去年订单量最多的城市，可能是：A,C,D三座城市，则取A,B,C三座城市名称及其去年的订单总量数据。
	 * @param mostTotalPd 统计参数，如统计的某年年份
	 * @return 最近2年订单量最多的城市及订单总量
	 */
	public List<PageData> listMostOrderTotalByYear(PageData mostTotalPd){
		Tools.initThisLastParam(mostTotalPd);
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listMostOrderTotalByYear", mostTotalPd);
	}

}